

import Axios from 'axios';
var http = require("http");

const PLAY_ADDR = "http://127.0.0.1:38080/"
function isInLAN(ip) {
    return ip && ip.indexOf("192.") != -1 || ip.indexOf("172.") != -1 || ip.indexOf("10.") != -1
}
function createConnection(){
    
    var axiosLocal = Axios.create({ baseURL: PLAY_ADDR, httpAgent: new http.Agent({ keepAlive: false }) })
    return axiosLocal
}
export function move(pos){
    if(pos){
        let posT = {}
        let r = detectZoom()
        posT.x = pos.x * r /100
        posT.y = pos.y * r/ 100
        posT.w = pos.width * r/ 100
        posT.h = pos.height * r/ 100
        posT.title = pos.title
    
        console.log("***********************!!!!!!!!",pos,r)
        let addrOption = {
            count: 1,
            hide: "false",
            pos: posT,
        }
        var axiosLocal = Axios.create({ baseURL: PLAY_ADDR })
        axiosLocal.post(PLAY_ADDR, addrOption).then((res) => {
            console.log("get data from local player", res)
        }).catch(err => {
            console.log("error -----play----", err)
        })
    }
   
}
export function hidePlayer(){
    let addrOption = {
        hide: "true",
    }
    var axiosLocal = createConnection()
    axiosLocal.post(PLAY_ADDR, addrOption).then((res) => {
        console.log("get data from local player", res)
    }).catch(err => {
        console.log("error -----play----", err)
    })
}

export function playVideo(tname, streamName, edgeId, rtspAddr, isCloud, pos) {
    //isCloud则获取设备的edgeid，获取设备的videoURL（raw）
    //发送api/begin命令，等待打洞结果
    //如果打洞成功则进行播放,如果打洞失败，显示打洞失败

    let r = detectZoom()
    pos.x = pos.x * r /100
    pos.y = pos.y * r/ 100
    pos.w = pos.w * r/ 100
    pos.h = pos.h * r/ 100

    console.log("***********************!!!!!!!!",pos,r)
    let addrOption = {
        count: 1,
        hide: "false",
        pos: pos,
        list:
            [
                { screen: 1, rtsp: rtspAddr, "name": streamName + "@" + tname }
            ]
    }
    if(isCloud){
        addrOption.list[0].edgeID = edgeId
    }
    

    var axiosLocal = createConnection()
    axiosLocal.post(PLAY_ADDR, addrOption).then((res) => {
        console.log("get data from local player", res)
    }).catch(err => {
        console.log("error -----play----", err)
    })



}
export function stopVideo() {
    //isCloud则获取设备的edgeid，获取设备的videoURL（raw）
    //发送api/begin命令，等待打洞结果
    //如果打洞成功则进行播放,如果打洞失败，显示打洞失败

    let addrOption = {
        count: 1,
        hide: "true",
       
    }
    
    

    var axiosLocal = createConnection()
    axiosLocal.post(PLAY_ADDR, addrOption).then((res) => {
        console.log("get data from local player", res)
    }).catch(err => {
        console.log("error -----play----", err)
    })



}

function detectZoom() {
    var ratio=0;
    var screen=window.screen;
    var ua=navigator.userAgent.toLowerCase();

    if(window.devicePixelRatio !== undefined)
    {
        ratio=window.devicePixelRatio;    
    }
    else if(~ua.indexOf('msie'))
    {
        if(screen.deviceXDPI && screen.logicalXDPI)
        {
            ratio=screen.deviceXDPI/screen.logicalXDPI;        
        }
    
    }
    else if(window.outerWidth !== undefined && window.innerWidth !== undefined)
    {
        ratio=window.outerWidth/window.innerWidth;
    }

    if(ratio)
    {
        ratio=Math.round(ratio*100);    
    }
    return ratio;
}
export default {
    playVideo,
    stopVideo,
    hidePlayer
}